package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.CollectMapper;
import org.linlinjava.litemall.db.models.Collect;
import org.linlinjava.litemall.db.service.CollectService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
        implements CollectService {

    @Override
    public int count(Long uid, byte type, Long gid) {
        Wrapper<Collect> query = new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUserId, uid)
                .eq(Collect::getType, type)
                .eq(Collect::getValueId, gid);
        return this.baseMapper.selectCount(query);
    }

    @Override
    public IPage<Collect> queryByType(Long userId, Byte type, Integer pageNo, Integer pageSize) {
        Page<Collect> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Collect> query = new QueryWrapper<>();
        query.eq(type!=null, "type", type);
        query.eq("user_id", userId);
        return this.baseMapper.selectPage(page, query);
    }

    @Override
    public int countByType(Long userId, Byte type) {
        Wrapper<Collect> query = new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUserId, userId)
                .eq(Collect::getType, type);
        return this.baseMapper.selectCount(query);
    }

    @Override
    public Collect queryByTypeAndValue(Long userId, Byte type, Integer valueId) {
        Wrapper<Collect> query = new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUserId, userId)
                .eq(Collect::getType, type)
                .eq(Collect::getValueId, valueId);
        return this.baseMapper.selectOne(query);
    }

    @Override
    public IPage<Collect> querySelective(Long userId, String valueId, Integer pageNo, Integer pageSize) {
        Page<Collect> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Collect> query = new QueryWrapper<>();
        query.eq(userId != null, "user_id", userId);
        query.eq(!StringUtils.isEmpty(valueId), "value_id", valueId);
        return this.baseMapper.selectPage(page, query);
    }
}
